package com.mall.common.domain;

import com.mall.common.constant.HttpStatus;

import java.io.Serializable;
import java.util.HashMap;

public class Result extends HashMap<String, Object> implements Serializable {
    private final String MSG = "msg";
    private final String CODE = "code";
    private final String DATA = "data";

    public Result() {}
    public Result(int code,String msg, Object data) {
        this.put(MSG, msg);
        this.put(CODE, code);
        if (data != null) {
            this.put(DATA, data);
        }
    }

    /**
     * 返回正常信息
     * @param msg
     * @return
     */
    public static Result ok (String msg){
        return ok(msg, null);
    }

    public static Result ok (Object data){
        return ok("操作成功", data);
    }

    public static Result ok (String msg,Object data){
        return ok(HttpStatus.success,msg,data);
    }

    public static Result ok (int code,String msg,Object data){
        return new Result(code,msg,data);
    }

    /**
     * 返回报错信息
     * @param msg
     * @return
     */
    public static Result error(String msg){
        return error(HttpStatus.error,msg);
    }

    public static Result error(int code,Object data){
        return error(code,"操作失败",data);
    }

    public static Result error(int code,String msg){
        return error(code,msg,null);
    }

    public static Result error(int code,String msg,Object data){
        return new Result(code,msg,data);
    }

    @Override
    public Object put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
